package com.fit.networkShop.pages;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.networkShop.beans.Country;
import com.fit.networkShop.beans.Gender;
import com.fit.networkShop.beans.UserBean;
import com.fit.networkShop.interfaces.User;
import com.fit.networkShop.service.Customers;
import com.fit.networkShop.service.Users;
import com.fit.networkShop.service.UsersBean;



public class Registration {
	@Inject
	private Users users;
	@Inject
	private Customers customers;

	@Component(id = "registrationForm")
	private Form registrationForm;
	@Component(id = "password")
	private PasswordField passwordField;
	@Component(id = "userName")
	private TextField userNameField;
	@Inject
	private Messages messages;

	@Persist
	private String userName;
	@Persist
	private String password;
	@Persist
	private String password2;
	@Persist
	private Gender gender; // = Gender.FEMALE;
	@Persist
	private boolean subscribe;
	@Persist
	private String email;
	@Persist
	private Country country;
	@Persist
	private double age;
	private boolean unsubscribe;
	private Class nextPage;
	private boolean isSubmited = false;

	void onActivate() {
		if (this.gender == null) this.gender = Gender.MALE;
	}
	
	Object onSubmitFromRegistrationForm()
	{
		if (unsubscribe) subscribe = false;
		return nextPage;
	}

	void onValidateForm() {
		if (!((userName == null) || (password == null) || (password2 == null)))
		{
			if (!password.equals(password2)) {
				password = null;
				System.out.println("passwords-dont-match");
				registrationForm.recordError(passwordField, messages.get("passwords-dont-match"));
			}
			if (users.retrieveSingleUser(getUserName()) != null)
			{
				System.out.println("userName-exists");
				registrationForm.recordError(userNameField, messages.get("userName-exists"));
			}
		}
	}
	
	void onSuccess() {
		if (this.isSubmited == false) return;
		User userBean = new UserBean();
		userBean.executeUserCreation(getUserName(), getPassword(), getGender(), isSubscribe(), getEmail(), getCountry(), getAge(), users, customers);		
		userName = null;
		password = null;
		password2 = null;
		email = null;
		gender = null;
		subscribe = false;
		nextPage = Index.class;
	}

	@OnEvent(value="selected", component="submitButton")
	void onSubmitButton()
	{
		this.isSubmited = true;
	}

	@OnEvent(value="selected", component="resetButton")
	void onResetButton() { 
		System.out.println("Resetting...");
		registrationForm.clearErrors();
		userName = null;
		password = null;
		password2 = null;
		email = null;
		gender = null;
		subscribe = false;
	}
	
	public boolean isPasswordNotSubmitted() {
		if (password != null) System.out.println("Provjera2: " + ((!password.equals(password2))));
		return (password == null) || (!password.equals(password2));
	}

	public SelectModel getCountries() {
		return new EnumSelectModel(Country.class, messages);
	}

	public Country getCountry() {
		return country;
	}
	public void setCountry(Country country) {
		this.country = country;
	}

	public boolean isUnsubscribe() {
		return unsubscribe;
	}
	public void setUnsubscribe(boolean unsubscribe) {
		this.unsubscribe = unsubscribe;
	}

	public boolean isSubscribe() {
		return subscribe;
	}
	public void setSubscribe(boolean subscribe) {
		this.subscribe = subscribe;
	}

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public Gender getMale() {
		return Gender.MALE;
	}
	public Gender getFemale() {
		return Gender.FEMALE;
	}

	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public double getAge() {
		return age;
	}
	public void setAge(double age) {
		this.age = age;
	}
}
